﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows;
using System.IO;

namespace QLKS.Models
{
    public static class ThuePhongModel
    {
        public static DataTable GetAllPhieuThuePhong(string selectColumns)
        {
            return DataProviderModel.QueryResults(string.Format("select {0} from ThuePhong", selectColumns.Trim() == "" ? "*" : selectColumns)).Tables[0];
        }

        //Tạo phòng mới
        public static DataTable CreateNewThuePhong(string selectColumns)
        {
            return DataProviderModel.QueryResults(string.Format("select {0} from ThuePhong WHERE 1=0", selectColumns.Trim() == "" ? "*" : selectColumns)).Tables[0];
      
        }

        //Lấy tất cả phiếu thuê phòng
        public static DataTable GetAllChiTietPhieuThuePhong(string selectColumns)
        {
            return DataProviderModel.QueryResults(string.Format("select {0} from ChiTietThuePhong", selectColumns.Trim() == "" ? "*" : selectColumns)).Tables[0];
        }

        //Lấy chi tiết thuê phòng
        public static DataTable GetChiTietPhieuThue(string maphong, string selectColumns)
        {
            return DataProviderModel.QueryResults(string.Format("Exec dbo.sp_chitietphieuthue '{0}','{1}'", maphong ,selectColumns.Trim() == "" ? "*" : selectColumns)).Tables[0];
        }

        //Lấy chi tiết thuê phòng
        public static DataTable GetChiTietPhongThue(string maphong, string selectColumns)
        {
            return DataProviderModel.QueryResults(string.Format("Exec dbo.sp_chitietphongthue '{0}','{1}'", maphong, selectColumns.Trim() == "" ? "*" : selectColumns)).Tables[0];
        }

        public static int GetIdentity()
        {
            return (int)DataProviderModel.ScalarExecute("select Max(matp) from ThuePhong");
        }
        //Thêm Phiếu thuê phòng
        public static int ThemPhieuThuePhong(int ma, string maphong, DateTime ngaythue, TimeSpan giothue, int loaithue)
        {
            int maphongnew = -1;
            DataTable tb = DataProviderModel.CreateColumns("ThuePhong", "matp");
            tb.Rows.Add(maphong, ngaythue, giothue, null, null, loaithue, null,null,0);
            if (ma < 0)
            {
                DataProviderModel.Insert(tb, "ThuePhong", "matp");
                maphongnew = GetIdentity();
            }
            else
                return DataProviderModel.Update(tb, "ThuePhong", "matp");
            return maphongnew;
        }

        //Thêm chi tiết thuê phòng
        public static int ThemChiTietThuePhong(int maphieuthuephong, List<string> maKhachHang)
        {
            DataTable tb = DataProviderModel.CreateColumns("ChiTietThuePhong", "ma");
            foreach (string kh in maKhachHang)
                tb.Rows.Add(maphieuthuephong, kh);
            if (GetAllChiTietPhieuThuePhong("maphieuthue").Select("maphieuthue =" + maphieuthuephong).Length <= 0)
            {
                return DataProviderModel.Insert(tb, "ChiTietThuePhong", "ma");
            }
            else
            {
                DataProviderModel.RunExecute("Delete ChiTietThuePhong Where maphieuthue =" + maphieuthuephong);
                return DataProviderModel.Insert(tb, "ChiTietThuePhong", "");
            }
        }
    }
}
